package lesson4;

import java.util.Scanner;

public class Task3 {

		public static void main(String[] args) {

			Scanner s = new Scanner(System.in);
			System.out.println("Enter number of days");
			int n = s.nextInt();

			double[] temperatures = new double[n];
			
			for(int i = 0; i < temperatures.length; i++)
			{
				System.out.println("Enter temperature for day " + (i+1));
				temperatures[i] = s.nextDouble();
			}
			//calculate avg temp
			double sum = 0;
			for(int i = 0; i < temperatures.length; i++)
				sum += temperatures[i];
			
			double avgTemp = sum/temperatures.length;
			
			System.out.println("Avarage temp = " + avgTemp);
			
			//calculate max consecutive days under avg
			int maxFreezeDays  = 0;
			int counter = 0;
			
			int lastIndex = 0;
			
			for(int i = 0; i < temperatures.length ; i++)
			{
				if(temperatures[i] < avgTemp)
				{
					counter++;
					if(counter > maxFreezeDays)
					{
						maxFreezeDays = counter;
						lastIndex = i;
					}
				}
				else
					counter = 0;
			}
			
			//last index = the last element of desired subArray
			//maxFreezeDays = the length of the desired subArray
			
			System.out.println("Desired subArray of below avg temps");
			for(int i = lastIndex-maxFreezeDays+1; i<=lastIndex;i++ )
				System.out.print(temperatures[i] + " ");
			System.out.println();

			//calc max consecutive days when progressive freeze occurs
			int maxFreezingDays  = 0;
			int counter2 = 1;
			
			for(int i = 1; i < temperatures.length ; i++)
			{
				if(temperatures[i] < temperatures[i-1])
				{
					counter2++;
					if(counter2 > maxFreezingDays)
					{
						maxFreezingDays = counter2;
					}
				}
				else
					counter2 = 1;
			}
			
			System.out.println("Max consecutive days under avg temp = " + maxFreezeDays);

			System.out.println("Max consecutive freezing days = " + maxFreezingDays);


			//calc max consecutive days when progressive freeze occurs
		}
}
